//
//  CharListParser.m
//  iEVETracker
//
//  Created by Scott Guyer on 7/13/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "CharListParser.h"
#import "defs.h"


@implementation CharListParser



- (NSDictionary*) parseXml:(NSData*)xmlData 
{
	if ( _parser == nil )
	{
		_parser = [NSXMLParser alloc];
	}
	[_parser initWithData:xmlData];
	[_parser setDelegate:self];
	
	_charlist = [[NSMutableDictionary alloc] init];
	
	_processingChars = NO;
	if ( [_parser parse] )
	{
		return _charlist;
	}
	else
	{
		NSLog(@"error parsing character list xml: %@", 
			  [_parser parserError]);
		return nil;
	}
	
}



- (void) parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
   namespaceURI:(NSString *)namespaceURI 
  qualifiedName:(NSString *)qualifiedName 
	 attributes:(NSDictionary *)attributeDict
{
	if ( [elementName isEqualToString:@"rowset"] )
	{
		_processingChars = YES;
	}
	
	if ( _processingChars && [elementName isEqualToString:@"row"] )
	{
		NSString* name = [(NSString*)[attributeDict objectForKey:@"name"] copy];
		NSString* cid = [(NSString*)[attributeDict objectForKey:@"characterID"] copy];
		
		[_charlist setValue:cid forKey:name];
		
		[name release];
		[cid release];
	}
}


- (void) parser:(NSXMLParser *)parser 
  didEndElement:(NSString *)elementName 
   namespaceURI:(NSString *)namespaceURI 
  qualifiedName:(NSString *)qName
{
	if ( _processingChars && [elementName isEqualToString:@"rowset"] )
	{
		_processingChars = NO;
	}
}



- (void) dealloc
{
	NSLog(@"%s 0x%x", __FUNCTION__, self);
	
	WIPE( _parser)
	WIPE( _charlist)

	[super dealloc];
}

@end
